package com.hsc.www.mybatis.test0;

import com.hsc.www.mybatis.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test0 {

    public static void main(String args[]) throws Exception {

        //mybatis的配置文件
        String resource = "test0/mybatis-conf.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        //通过 SqlSessionFactoryBuilder创建qlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        //获取 sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //方式1 执行 sql
        Map params = new HashMap();
        params.put("id", 1);
        params.put("name", "Jone");
        List<Student> studentList1 = sqlSession.selectList("com.hsc.www.mybatis.test0.StudentMapper.select", params);
        //selectUser为方法名
        studentList1.forEach(System.out::println);
        //方式2 执行 sql
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList2 = studentMapper.select(1, "Jone", null, null);
        studentList2.forEach(System.out::println);

        //关闭 sqlSession
        sqlSession.close();
    }


}
